package com.zzyy.study.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.zzyy.study.service.AiAssistant;
import com.zzyy.study.service.ChatAssistant;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.listener.ChatModelRequest;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.Response;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther zzyy
 * @create 2025-02-21 22:44
 */
@RestController
@Slf4j
public class ChatLanguageModelController
{
    @Resource
    private ChatAssistant chatAssistant;
    @Resource
    private AiAssistant aiAssistant;
    @Resource
    private ChatLanguageModel chatLanguageModel;


    @GetMapping(value = "/chatmemory/test1")
    public String testWithOutChatMemory()
    {

        String chat = chatAssistant.chat("你好，我的名字叫张三");
        System.out.println("chat返回结果："+chat);

        String chat2 = chatAssistant.chat("我的名字是什么");
        System.out.println("chat2返回结果："+chat2);

        return "success : "+ DateUtil.now();
    }

    @GetMapping(value = "/chatmemory/test2")
    public String testWithChatMemory()
    {
        aiAssistant.chat(1L, "你好！我的名字是Java.");
        aiAssistant.chat(3L, "你好！我的名字是C++");

        String chat = aiAssistant.chat(1L, "我的名字是什么");
        System.out.println(chat);
        chat = aiAssistant.chat(3L, "我的名字是什么");
        System.out.println(chat);

        return "testWithChatMemory success : "+ DateUtil.now();
    }

    @GetMapping(value = "/chatmemory/test3")
    public String testWithChatMemory3()
    {
        UserMessage userMessage1 = UserMessage.userMessage("你好，我叫java");
        ChatResponse chatResponse1 = chatLanguageModel.chat(new ChatRequest.Builder().messages(List.of(userMessage1)).build());
        AiMessage aiMessage1 = chatResponse1.aiMessage();
        System.out.println(aiMessage1.text());


        System.out.println("------------------");


        /**
         * 这个原生api超级麻烦，每次都要手动把上一次的对话带入下一次，不推荐
         */
        ChatRequest chatRequest = new ChatRequest.Builder().messages(List.of(userMessage1,
                aiMessage1, UserMessage.userMessage("我叫什么"))).build();

        ChatResponse chatRespons2 = chatLanguageModel.chat(chatRequest);
        System.out.println(chatRespons2.aiMessage().text());

        return "testWithChatMemory3 success : "+ DateUtil.now();
    }
}
